<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="ctypes-loading-shared-libraries.html" />
<link rel="prev" href="ctypes-ctypes-reference.html" />
<link rel="parent" href="ctypes-ctypes-reference.html" />
<link rel="next" href="ctypes-loading-shared-libraries.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>14.14.2.1 Finding shared libraries</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.14.2 ctypes reference"
  href="ctypes-ctypes-reference.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.14.2 ctypes reference"
  href="ctypes-ctypes-reference.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.14.2.2 loading shared libraries"
  href="ctypes-loading-shared-libraries.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="ctypes-ctypes-reference.html">14.14.2 ctypes reference</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="ctypes-ctypes-reference.html">14.14.2 ctypes reference</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="ctypes-loading-shared-libraries.html">14.14.2.2 Loading shared libraries</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h3><a name="SECTION00161421000000000000000"></a><a name="ctypes-finding-shared-libraries"></a>
<br>
14.14.2.1 Finding shared libraries
</h3>

<p>
When programming in a compiled language, shared libraries are accessed
when compiling/linking a program, and when the program is run.

<p>
The purpose of the <code>find_library</code> function is to locate a library in
a way similar to what the compiler does (on platforms with several
versions of a shared library the most recent should be loaded), while
the ctypes library loaders act like when a program is run, and call
the runtime loader directly.

<p>
The <code>ctypes.util</code> module provides a function which can help to
determine the library to load.

<p>
<dl><dt><b><tt>find_library(name)</tt></b></dt>
<dd>
Try to find a library and return a pathname.  <var>name</var> is the
library name without any prefix like <var>lib</var>, suffix like <code>.so</code>,
<code>.dylib</code> or version number (this is the form used for the posix
linker option <b class="programopt">-l</b>).  If no library can be found, returns
<code>None</code>.
</dd></dl>

<p>
The exact functionality is system dependend.

<p>
On Linux, <code>find_library</code> tries to run external programs
(/sbin/ldconfig, gcc, and objdump) to find the library file.  It
returns the filename of the library file.  Here are sone examples:
<div class="verbatim"><pre>
&gt;&gt;&gt; from ctypes.util import find_library
&gt;&gt;&gt; find_library("m")
'libm.so.6'
&gt;&gt;&gt; find_library("c")
'libc.so.6'
&gt;&gt;&gt; find_library("bz2")
'libbz2.so.1.0'
&gt;&gt;&gt;
</pre></div>

<p>
On OS X, <code>find_library</code> tries several predefined naming schemes and
paths to locate the library, and returns a full pathname if successfull:
<div class="verbatim"><pre>
&gt;&gt;&gt; from ctypes.util import find_library
&gt;&gt;&gt; find_library("c")
'/usr/lib/libc.dylib'
&gt;&gt;&gt; find_library("m")
'/usr/lib/libm.dylib'
&gt;&gt;&gt; find_library("bz2")
'/usr/lib/libbz2.dylib'
&gt;&gt;&gt; find_library("AGL")
'/System/Library/Frameworks/AGL.framework/AGL'
&gt;&gt;&gt;
</pre></div>

<p>
On Windows, <code>find_library</code> searches along the system search path,
and returns the full pathname, but since there is no predefined naming
scheme a call like <code>find_library("c")</code> will fail and return
<code>None</code>.

<p>
If wrapping a shared library with <code>ctypes</code>, it <em>may</em> be better to
determine the shared library name at development type, and hardcode
that into the wrapper module instead of using <code>find_library</code> to
locate the library at runtime.

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.14.2 ctypes reference"
  href="ctypes-ctypes-reference.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.14.2 ctypes reference"
  href="ctypes-ctypes-reference.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.14.2.2 loading shared libraries"
  href="ctypes-loading-shared-libraries.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="ctypes-ctypes-reference.html">14.14.2 ctypes reference</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="ctypes-ctypes-reference.html">14.14.2 ctypes reference</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="ctypes-loading-shared-libraries.html">14.14.2.2 Loading shared libraries</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
